<html>
<head><meta charset="utf-8"><title>Perform operation on `Const` · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html">Perform operation on `Const`</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212224618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212224618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212224618">(Oct 04 2020 at 14:02)</a>:</h4>
<p>If I have a <code>ty::Const</code> and its type is <code>Int</code>, for instance, what's the correct way to perform an operation like <code>abs</code> on it? I just realised I've only ever dealt with consts abstractly, rather than manipulating them.</p>



<a name="212224627"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212224627" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212224627">(Oct 04 2020 at 14:03)</a>:</h4>
<p>maybe by using <code>try_eval_usize</code> <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="212224657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212224657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212224657">(Oct 04 2020 at 14:04)</a>:</h4>
<p>or for other integers looking at <code>ConstKind::Value</code> after calling <code>const_eval_resolve</code></p>



<a name="212224692"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212224692" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212224692">(Oct 04 2020 at 14:04)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="124288">@oli</span> what's the best way to get the relevant integer rn while val tree is not yet implemented</p>



<a name="212225234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212225234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212225234">(Oct 04 2020 at 14:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="216206">lcnr</span> <a href="#narrow/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60/near/212224627">said</a>:</p>
<blockquote>
<p>maybe by using <code>try_eval_usize</code> <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>
</blockquote>
<p>Yeah, I was wondering whether there was a way to do it without evaluating, but maybe this is the most straightforward solution.</p>



<a name="212225252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212225252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212225252">(Oct 04 2020 at 14:19)</a>:</h4>
<p>What do you want the constant for? It seems weird to me to do anything with consts without first trying to evaluate them</p>



<a name="212225290"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212225290" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212225290">(Oct 04 2020 at 14:20)</a>:</h4>
<p>esp when dealing with lazy normalization</p>



<a name="212225435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212225435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212225435">(Oct 04 2020 at 14:24)</a>:</h4>
<p>In the particular case I'm interested in, I can evaluate the constant, because I just want to produce the right string for the v0 mangling, but I was wondering how to go about it when that wasn't possible. Maybe the answer is: "You never want to do this in practice anyway." :P</p>



<a name="212225541"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212225541" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212225541">(Oct 04 2020 at 14:27)</a>:</h4>
<p>"You never want to do this in practice anyway."</p>
<p><span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span> I think that this is the case, but maybe some forgotten arcane magic does require this</p>



<a name="212226545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226545">(Oct 04 2020 at 14:56)</a>:</h4>
<p>You don't want do do this in practice indeed</p>



<a name="212226606"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226606" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226606">(Oct 04 2020 at 14:58)</a>:</h4>
<p>Have a look at the soon-to-be-removed const_deref (or deref_const? I can never remember) on how to do ops via the mir interpreter on arbitrary constants. But yea, I highly recommend not to do that and please ping me on any PRs that even remotely do something like that</p>



<a name="212226616"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226616" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226616">(Oct 04 2020 at 14:58)</a>:</h4>
<p>Also look at my scalar int PR for some stuff that should make const generics simpler soon</p>



<a name="212226621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226621">(Oct 04 2020 at 14:58)</a>:</h4>
<p>Value trees are coming</p>



<a name="212226631"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226631" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226631">(Oct 04 2020 at 14:59)</a>:</h4>
<p>Winter, too, but that's a different story</p>



<a name="212226639"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226639" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226639">(Oct 04 2020 at 14:59)</a>:</h4>
<p>what's the scalar int PR?</p>



<a name="212226711"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226711" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226711">(Oct 04 2020 at 15:00)</a>:</h4>
<p><a href="https://github.com/rust-lang/rust/pull/77227">https://github.com/rust-lang/rust/pull/77227</a></p>



<a name="212226747"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226747" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226747">(Oct 04 2020 at 15:01)</a>:</h4>
<p>Oh wow, look at these beautiful numbers <a href="https://github.com/rust-lang/rust/pull/77390">https://github.com/rust-lang/rust/pull/77390</a></p>



<a name="212226755"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226755" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226755">(Oct 04 2020 at 15:01)</a>:</h4>
<p>So much code removed and we get a perf improvement</p>



<a name="212226823"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212226823" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212226823">(Oct 04 2020 at 15:03)</a>:</h4>
<p>(The latter PR is assigned to you <span class="user-mention" data-user-id="121053">@varkor</span> btw, but feel free to reassign)</p>



<a name="212227843"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212227843" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212227843">(Oct 04 2020 at 15:33)</a>:</h4>
<p>these are some nice PRs if only I were comfortable with reviewing them <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="212241708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Perform%20operation%20on%20%60Const%60/near/212241708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> varkor <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60.html#212241708">(Oct 04 2020 at 21:03)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/131828-t-compiler/topic/Perform.20operation.20on.20.60Const.60/near/212226823">said</a>:</p>
<blockquote>
<p>(The latter PR is assigned to you <span class="user-mention silent" data-user-id="121053">varkor</span> btw, but feel free to reassign)</p>
</blockquote>
<p>It's on my radar: I just need to schedule some time to look at it properly. I'll try to get to it soon!</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>